package com.demo.cms.action.user;


import java.util.List;

import com.demo.Constants;
import com.demo.cms.model.Article;
import com.demo.core.model.Template;
import com.demo.webapp.action.TemplateProvider;

/**
 * 检索Action
 * 前端页面展示的Acttion,该检索可跨频道检索
 * <br>根据检索条件生成结果(基于Lecene的检索开发)
 * <br>实现接口TemplateProvider,提供自渲染的页面展示
 * @author ryuu.kk
 *
 */
public class SearchAction extends ClientBaseAction implements TemplateProvider {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 5457908809351588642L;

	/**
	 * 检索范围
	 */
	private String scope;
	
	/**
	 * 检索关键字
	 */
	private String query;
	
	/**
	 * 模板路径
	 */
	private String path;
	
	/**
	 * 模板模型
	 */
	private Template template;
	
	/**
	 * 模板渲染ID
	 */
	private Long templateId;
	
	/**
	 * 文章列表
	 */
	private List<Article> articleList;
	
	@Override
	public String execute() {
		
		// 请求的前期处理
		super.prepare();
				
//		Map<String, Object> params = new HashMap<String, Object>();
//		try {
//			query = new String(query.trim().getBytes("ISO-8859-1"), "UTF-8");
//		} catch (UnsupportedEncodingException e) {} 
//		params.put("siteId", siteId);
//		params.put("channelPath", channelPath);
//		//params.put("categoryPath", scope);
//		params.put("title", query);
//		params.put("description", query);
//		params.put("content", query);
//		
//		SearchArgumentSupport args = getArgumentSupport();
//		PageResultSupport<Article> resultSupport = queryService.queryList("article", params, Article.class, 
//				args.getFirstResult(), args.getMaxResults(), 
//				BaseFragmentsBuilder.COLORED_PRE_TAGS, BaseFragmentsBuilder.COLORED_POST_TAGS);
//		this.articleList = new ArrayList<Article>(resultSupport.getResults());
//		super.setResultSupport(new ResultSearchResult<Article>(resultSupport.getResults(), resultSupport.getTotalResults()));
//		// 取得渲染模板
		template = findTemplate(getTemplateId());
		if (template == null) {
			addActionError(getText("template.notfound", "没有找到指定的模板."));
			return CALLBACK;
		}
		return SUCCESS;
	}
	
	// ~~ setter here ============================
	
	public String getActionType() {
		return Constants.ACTION_TYPE_SEARCH;
	}
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}

	public List<Article> getArticleList() {
		return articleList;
	}

	public Template getTemplate() {
		return template;
	}

	@Override
	public Template getTemplateModel() {
		return this.template;
	}
}
